package com.smartwebx.fr.util;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.UUID;

import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
import com.smartwebx.fr.bean.ChartImgPath;
import com.smartwebx.fr.bean.ImgPath;

public class PathUtil {

	public static ImgPath getImgPathInfo(String reqUrl, Integer pageId) throws IOException {
		return getChartImgPathInfo(reqUrl, pageId, null);
	}

	public static ChartImgPath getChartImgPathInfo(String reqUrlStr, Integer pageId, String chartId)
			throws IOException {
		String rootName = StationFinalFields.CHART_IMAGE_ROOT_NAME;

		URL reqUrl = new URL(reqUrlStr);
		String imgBaseUrl = reqUrl.getProtocol() + "://" + reqUrl.getHost() + ":" + reqUrl.getPort() + "/" + rootName
				+ "/";
		String serverName = Iterables.get(Splitter.on('/').omitEmptyStrings().split(reqUrl.getPath()), 0);
		String serverBaseUrl = reqUrl.getProtocol() + "://" + reqUrl.getHost() + ":" + reqUrl.getPort() + "/"
				+ serverName + "/";

		String chartReqOptionUrl = reqUrlStr;
		chartReqOptionUrl = serverBaseUrl + "chart/getChartOption?chartId=" + chartId;

		// 项目路径
		String baseRootPath = System.getProperty("java.io.tmpdir");
		String pngName = chartId == null ? UUID.randomUUID().toString() : chartId;
		String filePath = rootName + "/" + pageId + "/" + pngName + ".png";
		File fullFile = new File(baseRootPath, filePath);
		String fullImgPath = fullFile.getPath();

		ChartImgPath chartImg = new ChartImgPath();
		chartImg.setFullImgPath(fullImgPath);
		chartImg.setImgBaseUrl(imgBaseUrl);
		chartImg.setChartReqOptionUrl(chartReqOptionUrl);
		chartImg.setImgName(pngName);
		return chartImg;
	}

}
